package com.example.study.controller;


import com.example.study.dao.User;
import com.example.study.service.UserService;
import com.example.study.utils.JsonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("/user")
public class UserController {

    private final static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    UserService userService;

    @GetMapping("/hello")
    public String test() {
        return "hello world";
    }

    @GetMapping(value = "/user/{id}")
    public JsonResult testPathVariable(@PathVariable Integer id) {
        return new JsonResult<>(id, "获取的id是" + id);
    }


    @GetMapping(value = "/user/{id}/{name}")
    public JsonResult test02PathVariable(@PathVariable Integer id, @PathVariable String name) {
        return new JsonResult<>(1, "获取的id是" + id + "获取的name值是" + name);
    }

    @RequestMapping("/user/{idd}")
    public String test03PathVariable(@PathVariable(value = "idd") Integer id) {
        System.out.println("获取到的id为：" + id);
        return "success";
    }


    @GetMapping(value = "/user")
    public JsonResult testRequestParam(@RequestParam Integer id) {
        return new JsonResult<>(1, "获取的id是" + id);
    }

    @RequestMapping("/user")
    public JsonResult test01RequestParam(@RequestParam(value = "idd", required = false) Integer id) {
        return new JsonResult<>(1, "获取的id是" + id);
    }


    @PostMapping("/testException")
    public JsonResult test(@RequestParam("name") String name,
                           @RequestParam("pass") String pass) {
        logger.info("name：{}", name);
        logger.info("pass：{}", pass);
        return new JsonResult();
    }

    @GetMapping("/getUserByName/{username}")
    public JsonResult user(@PathVariable String username) {
        User user = userService.getUserByName(username);
        return new JsonResult<>(user);
    }

    @PostMapping("/adduser")
    public String addUser(@RequestBody User user) {
        if (null != user) {
            userService.insertUser(user);
            return "success";
        } else {
            return "false";
        }
    }


}
